package com.tg.app.activity.device.ui.cameraview;

import androidx.fragment.app.Fragment;

import com.appbase.custom.constant.CommonConstants;
import com.tange.base.toolkit.NetworkType;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceItem;
import com.tg.app.camera.Camera;
import com.tange.module.camera.hub.CameraHub;
import com.tg.data.media.OnICameraAVListener;
import com.tg.data.media.OnICameraListener;
import com.tg.data.helper.DeviceTypeHelper;
import com.tg.appcommon.listener.NetStateChangeObserver;
import com.tg.data.http.entity.AVFrames;


public abstract class CameraBaseFragment extends Fragment implements NetStateChangeObserver {
    private static final String TAG = "CameraBaseFragment";
    protected OnICameraAVListener onICameraAVListener;
    protected Camera mCamera;
    protected DeviceItem mDeviceItem;

    public void setDeviceItem(DeviceItem deviceItem) {
        this.mDeviceItem = deviceItem;
        if (mCamera == null){
            mCamera = CameraHub.getInstance().getCamera(mDeviceItem.uuid);
        }
        TGLog.i(TAG, "setDeviceItem mCamera = " + mCamera + ", mDeviceItem = " + mDeviceItem);
    }

    private OnICameraListener internalOnICameraListener;

    public void setOnICameraAVListener(OnICameraAVListener onICameraAVListener) {
        this.onICameraAVListener = onICameraAVListener;
        TGLog.d(TAG, "CameraBaseFragment setOnICameraAVListener "+ toString());
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mDeviceItem == null){
            TGLog.i(TAG, "CameraBaseFragment mDeviceItem == null");
            mDeviceItem = getArguments().getParcelable(CommonConstants.EXT_DEVICE_ITEM);
            TGLog.i(TAG, "CameraBaseFragment mDeviceItem2 == " + mDeviceItem);
        }
        if (mDeviceItem != null){
            mCamera = CameraHub.getInstance().getCamera(mDeviceItem.uuid);
        }

        TGLog.i(TAG, "onResume mCamera " + mCamera);
        setCameraListener();
    }

   protected abstract void configurationView(int orientation);
    protected void setEnterSetupCMD(){
        if (mCamera!=null&&mCamera.isConnected()&& DeviceTypeHelper.isBatteryDevice(mDeviceItem.device_type)){
            mCamera.setEnterSetupCMD();
        }
    }
    public void setCameraListener(){
        TGLog.i(TAG, "setCameraListener "+toString());

        if (internalOnICameraListener == null) {
            internalOnICameraListener = new OnICameraListener() {
                @Override
                public void receiveUpdateConnectStates(int state) {
                }
                @Override
                public void receiveIOCtrlData(int type, byte[] data) {
                    receiveData(type,data);
                }
            };
        }

        if (mCamera !=null){
            mCamera.registerICameraListener(internalOnICameraListener);

            mCamera.registerCameraAVListener(new OnICameraAVListener() {
                @Override
                public void receiveVideoData(AVFrames avFrame) {
                    if (onICameraAVListener!=null){
                        onICameraAVListener.receiveVideoData(avFrame);
                    }
                }

                @Override
                public void receiveSubVideoData(AVFrames avFrame) {
                    if (onICameraAVListener!=null){
                        onICameraAVListener.receiveSubVideoData(avFrame);
                    }
                }

                @Override
                public void receiveAudioData(AVFrames avFrame) {
                    if (onICameraAVListener!=null){
                        onICameraAVListener.receiveAudioData(avFrame);
                    }
                }
            });
        }else{
            TGLog.i(TAG, "setCameraListener failed");
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        if (mCamera!=null&&mCamera.isConnected()&& DeviceTypeHelper.isBatteryDevice(mDeviceItem.device_type)){
            mCamera.setLeaveSetupCMD();
        }
        unregisterICameraListener();
    }

    @Override
    public void onStop() {
        super.onStop();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    public void unregisterICameraListener() {
        if (mCamera != null) {
            mCamera.unregisterICameraListener(internalOnICameraListener);
        }
    }

    public void receiveData(int type, byte[] data){

    }

    @Override
    public void onNetDisconnected() {

    }

    @Override
    public void onNetConnected(NetworkType networkType) {

    }
}
